// merchants/checkList/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    parentUid: '',
    childUid: '',
    orderList: [],
    CurrentPage: 1,
    loadingMoreHidden: 1,
    isShowDetail: false,
    isCoupon: 0, //0总笔数，1优惠笔数
    timeType: 1,
    paytype: '',//支付方式
    endTime: '',
    employeeid: 0,
    isAll: 0,
    linetype: 1,//1线上支付2线下支付
    childType: 1,//2所有分店
  },

  getOrderList: function (isCoupon, childUid, timeType, endtime, paytype, employeeid, isAll, linetype) {
    let that = this;
    let parentUid = 0;
    if(isAll == 1 || that.data.childType == 2){
      parentUid = app.globalData.userInfo.Uid
    }
    wx.request({
      url: app.globalData.host + '/api/WxStore/GetOrderList',
      data:{
        uid: childUid,
        parentUid: parentUid,
        isCoupon: isCoupon,
        endtime: endtime,
        timeType: parseInt(timeType),
        paytype: paytype,
        page: that.data.CurrentPage,
        employeeId: employeeid,
        isAll: isAll,
        linetype: linetype,
        childType: that.data.childType
      },
      success: function(res) {
        if (res.data.Code == 0) {
          let orderList = res.data.Data.List;
          if (!orderList) {
            return;
          }
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.orderList;
            orderList = preGoods.concat(orderList);
          }
          for (let i = 0; i < orderList.length; i++) {
            orderList[i].isShowDetail = false;
            orderList[i].isClick = 0;
            orderList[i].PaySystemName = orderList[i].PaySystemName.trim();
          }
          that.setData({
            orderList: orderList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }

      }
    })
  },
  // 核销记录(线下核销)
  getCheckList: function (childUid, linetype, timeType, endTime, employeeid, isCoupon, isAll) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Shareholder/GetCheckList',
      data:{
        createUid: childUid,
        gradeId: 0,
        page: that.data.CurrentPage,
        linetype: linetype,
        endTime: endTime,
        timeType: timeType,
        employeeId: employeeid,
        isCoupon: isCoupon,
        isAll: isAll,
        childType: that.data.childType
      },
      success: function (res) {
        if (res.data.Code == 0) {
          let orderList = res.data.Data.List;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.orderList;
            orderList = preGoods.concat(orderList);
          }
          for (let i = 0; i < orderList.length; i++) {
            orderList[i].OrderAmount = parseFloat(orderList[i].PayMoney) + parseFloat(orderList[i].CheckMoney);
            orderList[i].isShowDetail = false;
          }
          that.setData({
            orderList: orderList,
            loadingMoreHidden: res.data.Data.PageModel.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  showDetail: function(e) {
    let currentid = e.currentTarget.dataset.couponid;
    let employeeId = e.currentTarget.dataset.employeeid;
    let list = this.data.orderList;
    for (let i = 0; i < list.length; i++) {
      if (list[i].Pid == currentid) {
        list[i].isShowDetail = !list[i].isShowDetail;
        if (list[i].isClick == 0){
          this.getCouponList(currentid, employeeId);
        }
      }
    }
    this.setData({
      orderList: list
    })
  },

  // 获取优惠券
  getCouponList: function (oid, employeeId){
    let that = this;
    let orderList = that.data.orderList;
    wx.request({
      url: app.globalData.host + '/api/WxStore/GetOrderCheck?oid=' + oid + '&employeeId=' + employeeId,
      success:function(res){
        if(res.data.Code==0){
          let couponList = res.data.Data.couponList;
          let employeeName = res.data.Data.employee.employeeName
          for(let i = 0; i < orderList.length; i++){
            if (orderList[i].Pid == oid){
              orderList[i].couponList = couponList;
              orderList[i].employeeName = employeeName;
              orderList[i].isClick = 1;
            }
          }
          that.setData({
            orderList: orderList
          })
        }
      }
    })
  },
  showDetail2: function (e) {
    let id = e.currentTarget.dataset.id;
    let list = this.data.orderList;
    for (let i = 0; i < list.length; i++) {
      if (list[i].Id == id){
        list[i].isShowDetail = !list[i].isShowDetail;
      }
    }
    this.setData({
      orderList: list
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let childType = 1;
    if(options.childType){
      childType = options.childType;
    }
    this.data.childType = childType;
    if (options.linetype==2){//线下支付
      this.getCheckList(options.childUid, options.linetype, options.timeType, options.endtime, options.employeeid, options.iscoupon,options.isAll);
    }else{
      this.getOrderList(options.iscoupon, options.childUid, options.timeType, options.endtime, options.paytype, options.employeeid, options.isAll, options.linetype);
    }
    
    this.setData({
      isCoupon: options.iscoupon,
      childUid: options.childUid,
      timeType: options.timeType,
      endTime: options.endtime,
      paytype: options.paytype,
      employeeid: options.employeeid,
      isAll: options.isAll,
      linetype:options.linetype
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  // 加载更多
  loadMore: function(e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      if(that.data.linetype==2){
        that.getCheckList(that.data.childUid, that.data.linetype, that.data.timeType, that.data.endTime, that.data.employeeid, that.data.isCoupon,that.data.isAll,);
      }else{
        that.getOrderList(that.data.isCoupon, that.data.childUid, that.data.timeType, that.data.endTime, that.data.paytype, that.data.employeeid, that.data.isAll, that.data.linetype);
      }
      
    } else {
      return
    }
  },
})